package com.oh.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.oh.database.CargoDao;
import com.oh.view.CargoAdapter;

/**
 * 
 * 应用主界面
 * 
 * 1.搜选货物 2.检测同步
 * 
 * @author Alex.Lu
 * 
 */
public class MainActivity extends Activity implements TextWatcher, OnItemClickListener, OnClickListener {
	/** Called when the activity is first created. */
	
	private static final int MENU_ITEM_LOGOUT = 0x1;
	private static final int MENU_ITEM_UPDATE = 0x2;
	private static final int MENU_ITEM_ABOUT = 0x3;
	
	private static final int QR_SEARCH_CODE = 1234;
	
	private EditText mCargoNameET;
	private ListView mCargoLV;
	private ImageButton mQRSearchBT;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		mCargoNameET = (EditText) findViewById(R.id.cargo_name);
		mCargoNameET.addTextChangedListener(this);

		mCargoLV = (ListView) findViewById(R.id.cargo_list);
		mCargoLV.setOnItemClickListener(this);
		
		mQRSearchBT = (ImageButton)findViewById(R.id.qr_search_btn);
		mQRSearchBT.setOnClickListener(this);
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (s != null && !s.toString().equals("")) {
			Cursor c = CargoDao.querySearchItems(this, s.toString());
			CargoAdapter adapter = new CargoAdapter(this, c);
			mCargoLV.setVisibility(View.VISIBLE);
			mCargoLV.setAdapter(adapter);
        } else {
        	mCargoLV.setVisibility(View.GONE);
        }
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(this, CargoActivity.class);
		startActivity(intent);
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int v_id = v.getId();
		
		switch (v_id) {
		case R.id.qr_search_btn:
			Intent intent = new Intent();
			intent.setClass(this, CamTestActivity.class);
			startActivityForResult(intent, QR_SEARCH_CODE);
			break;

		default:
			break;
		}
	}
	
	@Override 
	protected void onActivityResult(int requestCode,int resultCode,Intent data)  
	{  
		
	    if(requestCode==QR_SEARCH_CODE &&resultCode==RESULT_OK) {
	    	String text = data.getStringExtra(CamTestActivity.EXTRA_RESULTS);
	    	
	    }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, MENU_ITEM_UPDATE, 0, this.getResources().getString(R.string.btn_update));
		menu.add(0, MENU_ITEM_ABOUT, 1, this.getResources().getString(R.string.btn_about));
		menu.add(0, MENU_ITEM_LOGOUT, 2, this.getResources().getString(R.string.btn_logout));
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch (item.getItemId()) {
		case MENU_ITEM_LOGOUT:
			intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			finish();
			break;
		case MENU_ITEM_UPDATE:

			break;
		case MENU_ITEM_ABOUT:
			intent.setClass(this, AboutActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}